$(document).ready(function() {
    function fetchDataAndUpdate(globalName) {
        $('.dataSum').empty();
        $.get(`http://127.0.0.1:5000/task4/marketName=${globalName}`).done(function (data) {
            const dataSumTag = document.querySelector('.dataSum')
            const keys = ['日商', 'AC', 'TC', '销售'];
            keys.forEach(key => {
                const li = document.createElement('li');
                let value = data[key];
                if (key === '日商') {
                    // 转换为万元并保留两位小数
                    value = (value / 10000).toFixed(2);
                    li.textContent = `${value}万元`;
                } else if(key === '销售'){
                    li.textContent = `${value}万元`;
                } else{
                    li.textContent = value;
                }
                dataSumTag.appendChild(li);
            });
        })
    }
    fetchDataAndUpdate(window.globalName)
    $(document).on('globalNameChanged', function(event, globalName) {
        // 清除所有已存在的 li 标签
        fetchDataAndUpdate(window.globalName)
        
    })
})
